//获取滚动条当前的位置
function getScrollTop() {
    var scrollTop = 0;
    if(document.documentElement && document.documentElement.scrollTop) {
        scrollTop = document.documentElement.scrollTop;
    } else if(document.body) {
        scrollTop = document.body.scrollTop;
    }
    return scrollTop;
}
//获取当前可视范围的高度  
function getClientHeight() {
    var clientHeight = 0;
    if(document.body.clientHeight && document.documentElement.clientHeight) {
        clientHeight = Math.min(document.body.clientHeight, document.documentElement.clientHeight);
    } else {
        clientHeight = Math.max(document.body.clientHeight, document.documentElement.clientHeight);
    }
    return clientHeight;
}
//获取文档完整的高度 
function getScrollHeight() {
    return Math.max(document.body.scrollHeight, document.documentElement.scrollHeight);
}
//滚动事件触发
window.onscroll = function() {
    if(getScrollTop() + getClientHeight() == getScrollHeight()) {
//        console.log('下拉刷新了');
        //此处发起AJAX请求
        var user1=$('#useri').val();
        var state1=$('#statei').val();
//        alert(state1);
       $.ajax({
			url:"/chat/xiala",
			type:"POST",
			data : {
				orderShowState:state1
			},
			success:function(result){
				content=null;
				for (var i = 0; i < result.orderShow.length; i++){
					var po=result.orderShow[i];
					var pagType=null;var butContent=null;
					if(result.role==1){
						switch (po.orderState) {
						case 0:
							pagType="已下单";
							butContent='<p id="pbq">等待启动</p>'
								+'<p id="pbq2" onclick="cancel('+po.orderId+')">取消订单</p>';
							break;
						case 1:
							pagType="已取消";
							butContent='<p id="pbq">申请恢复</p>';
							break;
						case 2:
							pagType="授课中...";
							butContent='<p id="pbq">正在学习</p>';
							break;
						case 3:
							pagType="待付款...";
							butContent='<p onclick="type3('+po.orderId+')" id="pbq">确认付款</p>';
							break;
						case 4:
							pagType="待评论...";
							butContent='<p id="pbq" class="showModal" onclick="type4('+po.orderId+');">添加评论</p>';
							break;
						case 6:
							pagType="已完成";
							butContent='<p id="pbq">已完成</p>';
							break;
						}
					}else if(result.role==2){
						switch (po.orderState) {
						case 0:
							pagType="已下单";
							butContent='<p onclick="type1('+po.orderId+')" id="pbq">启动课程</p>'
									  +'<p id="pbq2" onclick="cancel('+po.orderId+')">取消订单</p>';
							break;
						case 1:
							pagType="已取消";
							butContent='<p id="pbq">申请恢复</p>';
							break;
						case 2:
							pagType="授课中...";
							butContent='<p onclick="type2('+po.orderId+')" id="pbq">授课完成</p>';
							break;
						case 3:
							pagType="等待学员付款...";
							butContent='<p id="pbq">等待学员付款</p>';
							break;
						case 4:
							pagType="待评论...";
							butContent='<p id="pbq">等待学员评价</p>';
							break;
						case 6:
							pagType="已完成";
							butContent='<p id="pbq">已完成</p>';
							break;
						}
					}
					content+='<div id="pag" th:each="mes:${orderShow}">'
								+'<div id="pag-head" onclick="detail('+po.orderId+')">'+po.courseName+''
								+'</div>'
								+'<img src="img/右箭头.png" >'
								+'<div id="pag-type">'+pagType+''
								+'</div>'
								+'<div id="cut-off"></div>'
								+'<div id="portrait">'
								+'<div id="toux-box">'
								+'<img src="'+po.userHeadimg+'">'
								+'</div>'
								+'<div id="portrait-box">'
								+'订单时间:'+po.createTime+''
								+'</div>'
								+'<div id="portrait-box">'
								+''+po.orderMoney+'&nbsp;&nbsp;&nbsp;&nbsp;软妹币'
								+'</div>'
								+'</div>'
								+'<div id="butt">'+butContent+''
								+'</div>'
								+'</div>';
				}
//				alert(content);
				$("#contents").append(content);
			}
			
		});
       var modal = new RModal(document.getElementById('modal'), {
           beforeOpen: function(next) {
               console.log('beforeOpen');
               next();
           }
           , afterOpen: function() {
               console.log('opened');
           }

           , beforeClose: function(next) {
               console.log('beforeClose');
               next();
           }
           , afterClose: function() {
               console.log('closed');
           }
       });

       document.addEventListener('keydown', function(ev) {
           modal.keydown(ev);
       }, false);
       var elements = document.getElementsByClassName("showModal");
		 for(var i = 0;i < elements.length;i++){
			elements[i].addEventListener("click", function(ev) {
				    ev.preventDefault();
				    modal.open();
			}, false);
		}
       window.modal = modal;
    }
}

//var wrapTop =document.getElementById('scrollWrap')
//console.log(wrapTop.scrollTop + " " + "滚动条当前的位置")
//console.log(wrapTop.scrollHeight + " " + "获取滚动条的高度")


$(function(){
    window.onload = function() {
        var modal = new RModal(document.getElementById('modal'), {
            beforeOpen: function(next) {
                console.log('beforeOpen');
                next();
            }
            , afterOpen: function() {
                console.log('opened');
            }

            , beforeClose: function(next) {
                console.log('beforeClose');
                next();
            }
            , afterClose: function() {
                console.log('closed');
            }
        });

        document.addEventListener('keydown', function(ev) {
            modal.keydown(ev);
        }, false);
        
        var elements = document.getElementsByClassName("showModal");
		 for(var i = 0;i < elements.length;i++){
			elements[i].addEventListener("click", function(ev) {
				    ev.preventDefault();
				    modal.open();
			}, false);
		}
        window.modal = modal;
    }
});